package com.bysj.servelt;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.bysj.model.Comment;
import com.bysj.model.User;
import com.bysj.service.CommentService;
import com.bysj.service.OrderService;

/**
 * Servlet implementation class OrderCommentAddServlet
 */
@WebServlet("/order_comment_add")
public class OrderCommentAddServlet extends HttpServlet {
	private OrderService oService = new OrderService();
	private CommentService cService = new CommentService();

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		User user = (User) request.getSession().getAttribute("user");
		if (user != null) {
			int orderId = Integer.parseInt(request.getParameter("orderId"));
			String content = request.getParameter("content");
			Comment vo = new Comment();
			vo.setContent(content);
			vo.setOrderId(orderId);
			vo.setUserId(user.getId());
			vo.setUsername(user.getName());

			if (cService.addComment(vo)) {
				oService.updateStatus(orderId, 5);
				request.getRequestDispatcher("/order_list?stattus=6").forward(request, response);
			} else {
				request.setAttribute("msg", "评论失败，请重新填写！");
				request.getRequestDispatcher("/comment.jsp").forward(request, response);
			}
		} else {
			request.setAttribute("msg", "登录过期，请重新登录！");
			request.getRequestDispatcher("/comment.jsp").forward(request, response);
		}
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request, response);
	}

}
